Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • foreach results as a single table*

    Hi everyone,

    I am calculating the percentiles of simulation data and would like to have the results as a single table instead of a table for each variable. I would like a single table so it is easier to make a graph.

    - V2 to V21 are timesteps

    Currently I am using this code:

    foreach v of varlist v* {
    centile `v' , centile(10 50 90)
    }

    The image below is a sample of what I am currently getting.

    Click image for larger version

Name:	multiple tables.png
Views:	1
Size:	60.8 KB
ID:	1647664


    Any help is much appreciated,
    Thank you

  • #2
    one way to do this,
    Code:
    tabstat v*, s(p10 p50 p90)

    Comment


    • #3
      actually, if you want to make a graph maybe it is useful to save results to variables,
      Code:
      sysuse auto, clear
      rename (price mpg rep78) (v1 v2 v3)
      
      gen n = _n
      gen c = .
      gen lb = .
      gen ub = . 
      
      local k=0
      foreach v of var v* {
      centile `v', centile(10 50 90)
      forvalues i = 1/3 {
      replace c = r(c_`i') if n == `i'+`k'
      replace lb = r(lb_`i') if n == `i'+`k'
      replace ub = r(ub_`i') if n == `i'+`k'
      }
      local k=`k'+3
      }
      list c lb ub in 1/9, sep(3)

      Comment


      • #4
        I am still not having much success saving results to variables. I followed the command you recommended but when trying to list them I get the error "observation numbers out of range". The only thing I have changed is the below:

        list c lb ub in 1/60, sep(3)

        as I have 20 variables.

        I believe the problem is because I only have 50 rows in my dataset (50 simulations) but not sure how to fix the problem. Could you please help me?

        Many thanks again,

        Comment


        • #5
          maybe add 10 empty observations at the end of the dataset,
          Code:
          insobs 10
          
          gen n = _n
          gen c = .
          gen lb = .
          gen ub = .
          
          local k=0
          foreach v of var v* {
          centile `v', centile(10 50 90)
          forvalues i = 1/3 {
          replace c = r(c_`i') if n == `i'+`k'
          replace lb = r(lb_`i') if n == `i'+`k'
          replace ub = r(ub_`i') if n == `i'+`k'
          }
          local k=`k'+3
          }
          list c lb ub in 1/60, sep(3)
          Last edited by Øyvind Snilsberg; 01 Feb 2022, 23:30.

          Comment

          Working...
          X